<template>
  <a-drawer
    title="新增企业"
    :maskClosable="false"
    width=650
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="companyAddVisiable"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-form-item label='企业名称' v-bind="formItemLayout">
        <a-input v-decorator="['companyName',
                   {rules: [
                    { required: true, message: '企业名称不能为空'},
                    { max: 20, message: '长度不能超过20个字符'}
                  ]}]"/>
      </a-form-item>
      <a-form-item label='统一信用码' v-bind="formItemLayout" >
        <a-input v-decorator="['unifiedCreditCode',
                   {rules: [
                    { max: 100, message: '长度不能超过100个字符'}
                  ]}]"/>
      </a-form-item>
      <a-form-item label='地址' v-bind="formItemLayout">
        <a-input v-decorator="['companyAddr',
                   {rules: [
                    { max: 100, message: '长度不能超过100个字符'}
                  ]}]"/>
      </a-form-item>
      <a-form-item label='企业性质' v-bind="formItemLayout">
        <a-select v-decorator="['companyNature',
          {rules: [{ required: true, message: '请选择企业性质' }]}]">
          <a-select-option value="1">国家行政企业</a-select-option>
          <a-select-option value="2">公私合作企业</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label='法人代表名字' v-bind="formItemLayout" style="width: 100%">
        <a-input v-decorator="['companyLegalPerson',
                  {rules: [
                    { required: true, message: '法人代表名字不能为空'},
                    { max: 100, message: '长度不能超过100个字符'}
                  ]}]"/>
      </a-form-item>
      <a-form-item label='电话号码' v-bind="formItemLayout">
        <a-input
          v-decorator="['companyTe', {rules: [
            { pattern: '^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$', message: '请输入正确的手机号'}
          ]}]"/>
      </a-form-item>
      <a-form-item label='传真号码' v-bind="formItemLayout">
        <a-input
          v-decorator="['companyFax', {initialValue:defaultCompanyFax},{rules: [
            { pattern: '/^(0?\d{2,3}\-)?[1-9]\d{6,7}(\-\d{1,4})?$/', message: '请输入正确的传真号码'}
          ]}]"/>
      </a-form-item>
      <a-form-item label='电邮' v-bind="formItemLayout">
        <a-input
          v-decorator="['companyEmail',{rules: [
            { type: 'email', message: '请输入正确的邮箱' },
            { max: 50, message: '长度不能超过50个字符'}
          ]}]"/>
      </a-form-item>
      <a-form-item label='生产许可编码' v-bind="formItemLayout">
        <a-input v-decorator="['companyLicense',
                   {rules: [
                    { max: 100, message: '长度不能超过100个字符'}
                  ]}]"/>
      </a-form-item>
      <a-form-item label='企业人数' v-bind="formItemLayout">
        <a-input-number v-model="company.companyPeople" style="width: 100%"/>
      </a-form-item>

      <a-form-item label='企业简介' v-bind="formItemLayout">
        <a-textarea
          :rows="4"
          v-decorator="[
          'companyMemo',
          {rules: [
            { max: 50, message: '长度不能超过50个字符'}
          ]}]">
        </a-textarea>
      </a-form-item>
      <a-form-item label='企业类别' v-bind="formItemLayout">
        <a-select v-decorator="['companyType',
          {rules: [{ required: true, message: '请选择企业类别' }]}]">
          <a-select-option value="1">有限责任公司</a-select-option>
          <a-select-option value="2">股份有限公司</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label='所属区域' v-bind="formItemLayout">
        <a-select
          showSearch
          placeholder="请选择所属区域"
          optionFilterProp="children"
          style="width: 200px"
          :filterOption="filterOption"
          v-decorator="['zoneId',{rules: [{ required: true, message: '请选择所属区域' }]}]">
          <a-select-option v-for="z in zoneData" :key="z.zoneId">{{z.zoneName}}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label='所属安监局' v-bind="formItemLayout">
        <a-select
          showSearch
          placeholder="请选择所属安监局"
          optionFilterProp="children"
          style="width: 200px"
          :filterOption="filterOption"
          v-decorator="['officeId',{rules: [{ required: true, message: '请选择所属安监局' }]}]">
          <a-select-option v-for="o in officeData" :key="o.officeId">{{o.officeName}}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label='经度' v-bind="formItemLayout">
        <a-input-number v-model="company.companyLon" style="width: 100%"/>
      </a-form-item>
      <a-form-item label='维度' v-bind="formItemLayout">
        <a-input-number v-model="company.companyLat" style="width: 100%"/>
      </a-form-item>
    </a-form>
    <div class="drawer-bootom-button">
      <a-popconfirm title="确定放弃编辑？" @confirm="onClose" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-drawer>
</template>
<script>
const formItemLayout = {
  labelCol: { span: 5 },
  wrapperCol: { span: 18 }
}
export default {
  name: 'CompanyAdd',
  props: {
    companyAddVisiable: {
      default: false
    }
  },
  data () {
    return {
      loading: false,
      formItemLayout,
      form: this.$form.createForm(this),
      company: {},
      defaultCompanyFax: '021-88888888-8888',
      checkedKeys: [],
      expandedKeys: [],
      zoneData: [],
      officeData: []
    }
  },
  methods: {
    reset () {
      this.loading = false
      this.expandedKeys = this.checkedKeys = []
      this.company = {}
      this.form.resetFields()
    },
    onClose () {
      this.reset()
      this.$emit('close')
    },
    handleCheck (checkedKeys) {
      this.checkedKeys = checkedKeys
    },
    handleExpand (expandedKeys) {
      this.expandedKeys = expandedKeys
    },
    handleSubmit () {
      this.form.validateFields((err, values) => {
        if (!err) {
          this.setCompanyFields()
          this.loading = true
          this.$post('company/addCompany', {
            ...this.company
          }).then(() => {
            this.reset()
            this.$emit('success')
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    setCompanyFields () {
      let values = this.form.getFieldsValue(['companyName', 'unifiedCreditCode', 'companyAddr', 'companyNature',
        'companyLegalPerson', 'companyTe', 'companyFax', 'companyEmail', 'companyLicense', 'companyMemo', 'companyType',
        'zoneId', 'officeId'])
      if (typeof values !== 'undefined') {
        Object.keys(values).forEach(_key => { this.company[_key] = values[_key] })
      }
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    }
  },
  watch: {
    companyAddVisiable () {
      if (this.companyAddVisiable) {
        this.$get('zone/select').then((r) => {
          this.zoneData = r.data.rows
        })
        this.$get('office').then((r) => {
          this.officeData = r.data.rows
        })
      }
    }
  }
}
</script>
